package com.alogic.swagger.core;

import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.commons.lang3.StringUtils;

/**
 * Properties
 *
 * @since 1.6.13.7 [20200812 duanyy] <br>
 */
public class SwaggerProperty extends Segment {
    private String $name;
    private String $ref;
    private String $title;
    private String $type = "string";
    private String $desc;
    private String $default;
    private String $deprecated;
    private String $nullable;
    private String $examples;
    private String $readOnly;
    private String $writeOnly;
    private String $format;

    public SwaggerProperty(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);
        $name = PropertiesConstants.getRaw(p,"name",$name);
        $ref = PropertiesConstants.getRaw(p,"ref",$ref);
        $title = PropertiesConstants.getRaw(p,"title",$title);
        $type = PropertiesConstants.getRaw(p,"type",$type);
        $desc = PropertiesConstants.getRaw(p,"description",$desc);
        $deprecated = PropertiesConstants.getRaw(p,"deprecated",$deprecated);
        $default = PropertiesConstants.getRaw(p,"default",$default);
        $nullable = PropertiesConstants.getRaw(p,"nullable",$nullable);
        $examples = PropertiesConstants.getRaw(p,"examples",$examples);
        $readOnly = PropertiesConstants.getRaw(p,"readOnly",$readOnly);
        $writeOnly = PropertiesConstants.getRaw(p,"writeOnly",$writeOnly);
        $format = PropertiesConstants.getRaw(p,"format",$format);
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        String name = PropertiesConstants.transform(ctx,$name,"");
        if (StringUtils.isNotEmpty(name)) {
            XsObject property = current.getObjectChild("properties",true);
            XsObject newObject = property.getObjectChild(name,true);
            String ref = PropertiesConstants.transform(ctx, $ref, "");
            if (StringUtils.isEmpty(ref)) {
                newObject.addProperty("title", PropertiesConstants.transform(ctx, $title, ""));
                newObject.addProperty("type", PropertiesConstants.transform(ctx, $type, ""));
                newObject.addProperty("description", PropertiesConstants.transform(ctx, $desc, ""));
                if (StringUtils.isNotEmpty(PropertiesConstants.transform(ctx,$deprecated,""))) {
                    newObject.addProperty("deprecated", PropertiesConstants.transform(ctx, $deprecated, false));
                }
                newObject.addProperty("default", PropertiesConstants.transform(ctx, $default, ""));
                newObject.addProperty("nullable", PropertiesConstants.transform(ctx, $nullable, ""));
                newObject.addProperty("readOnly", PropertiesConstants.transform(ctx, $readOnly, ""));
                newObject.addProperty("writeOnly", PropertiesConstants.transform(ctx, $writeOnly, ""));
                newObject.addProperty("examples", PropertiesConstants.transform(ctx, $examples, ""));
                newObject.addProperty("format", PropertiesConstants.transform(ctx, $format, ""));
                super.onExecute(root, newObject, ctx, watcher);
            } else {
                newObject.addProperty("$ref", ref);
            }
        }
    }
}